package com.example.safehome;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;

import androidx.appcompat.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity {

    // xml控件
    private EditText editTextUsername;
    private EditText editTextPassword;
    private EditText editTextConfirmPassword;
    private Button btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        editTextUsername = (EditText) findViewById(R.id.et_register_username);
        editTextPassword = (EditText) findViewById(R.id.et_register_password);
        editTextConfirmPassword = (EditText) findViewById(R.id.et_register_confirm_password);
        btnRegister = (Button) findViewById(R.id.btn_register_2);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 获得控件上的值
                String username = editTextUsername.getText().toString();
                String password = editTextPassword.getText().toString();
                String confirmPassword = editTextConfirmPassword.getText().toString();

                // 用户注册逻辑
                if (password.equals(confirmPassword)) {
                    Login login = new Login();
                    login.setUsername(username);
                    login.setPassword(password);
                    login.save();
                    Log.d("password", password);
                    Log.d("confirmPassword", confirmPassword);
                    Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                else {
                    Toast.makeText(getApplicationContext(), "两次输入的密码不一致", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
